package com.cqupt.controller;

import com.cqupt.paging.Paging;
import com.cqupt.pojo.Goods;
import com.cqupt.pojo.Package;
import com.cqupt.pojo.User;
import com.cqupt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 功能：获取用户信息，注册，查看套餐类型集合，查看套餐类型分页查询,
 * 查看用户余额，用户充值
 * 获取商品表，用户用积分购买商品，用户用余额订购套餐,获取商品订单表
 * 展示少量精品 积分查询
 * @author 蒋俊贤
 * @version 1.0
 */

@Controller

public class UserController {
    @Autowired
    private UserService userService;
    @CrossOrigin()
    @RequestMapping("/user/login")
    @ResponseBody
    //登录-完成
    public User getUserInfo(HttpServletRequest request){
//        user.setUsername("user2");
//        user.setUserpwd("123");
        String loginId = request.getParameter("loginId");
        String loginPwd = request.getParameter("loginPwd");
        User user =new User();
        user.setUsername(loginId);
        user.setUserpwd(loginPwd);



        System.out.println("******************");
        System.out.println(user.toString());
        User result = userService.getUserInfo(user);
        System.out.println(result);
        return result;

    }


    //注册用户
    //成功返回1，用户名相同返回2，手机号相同返回3】

    @RequestMapping("/user/regist")
    @ResponseBody
    public int userAdd(User user){
        //user.setUsername("user4");
        //user.setUserpwd("123");
        //user.setPhone("1582215203");
        //新用户默认为0 不要修改
        user.setMoney(0.00);
        user.setScore(0);
        return userService.userAdd(user);


    }

    @RequestMapping("/user/userUpdate")
    @ResponseBody
    //用户修改
    public int userUpdate(User user){
        //user.setId(4);
        //user.setUsername("user5");
        //user.setUserpwd("12345");
        //user.setPhone("1555455");
        return userService.userUpdate(user);
    }
    //查看用户余额
    @RequestMapping("/user/money")
    @ResponseBody
    public double getUserMoney(User user){
        //user.setId(3);
        return userService.getUserMoney(user);
    }
    @RequestMapping("/user/package")
    @ResponseBody
    //查看套餐类型
    public List getPackageList(){

        return userService.getPackageList();
    }
    @RequestMapping("/user/packageLimit")
    @ResponseBody
    //查看套餐类型分页查询
    public Paging getPackageList(Integer number, Double price1, Double price2){
        return userService.getPackageList(number,price1,price2);
    }
    @RequestMapping("/user/better")
    @ResponseBody
    //展示少量精品
    public List getBetter(){
        return userService.getBetter();
    }
    @RequestMapping("/user/charge")
    @ResponseBody
    //用户充值，添加充值表信息，改变用户余额
    //用户为空，或id为空返回0
    //成功返回1
    public int charge(Integer id,Double money2) {
        User user = new User();
        user.setId(id);
        System.out.println(user);
        System.out.println(money2);

        //money2 = 30.00;
        return userService.charge(user,money2);
    }
    @RequestMapping("/user/goods")
    @ResponseBody
    //获取商品表
    public List getGoodsList(){
        return userService.getGoodsList();
    }

    @RequestMapping("/user/goodsLimit")
    @ResponseBody
    //获取商品表分页
    public Paging getGoodsList(Integer number,Integer score1,Integer score2){

        return userService.getGoodsList(number,score1,score2);
    }

    @RequestMapping("/user/shopping")
    @ResponseBody
    //用户用积分购买商品，用户积分减少，商品订单表更新，商品库存减少
    //积分不够返回2，库存不够返回3，商品已经下架返回4
    //用户或商品id为空返还0
    //成功返还1
    public int shopping(Integer userid, Integer id, Integer number){
        System.out.println("********************************");
        System.out.println(userid);
        System.out.println(id);
        System.out.println(number);
        return userService.shopping(userid,id, number);
    }

    @RequestMapping("/user/subscribe")
    @ResponseBody
    //用户用余额订购套餐，用户余额减少，积分增加，套餐订单表更新，
    //积分不够返回2，库存不够返回3，商品已经下架返回4
    //用户或套餐id为空返回0;
    //成功返还1
    public int subscribe(Integer userid,Integer id){
//        System.out.println(userid);
//        System.out.println(id);
        return userService.subscribe(userid,id);
    }

    @RequestMapping("/user/goodsOrder")
    @ResponseBody
    //获取商品订单表
    public List getUserOrderList(){
        return userService.getUserOrderList();
    }


    @RequestMapping("/user/goodsOrderLimit")
    @ResponseBody
    //获取商品订单表分页
    //state 存在0,1,2
    public Paging getUserOrderList(Integer number){
        return userService.getUserOrderList(number);
    }

    @RequestMapping("/user/goodsOrderLike")
    @ResponseBody
    //获取商品订单表分页的模糊查询
    public Paging getUserOrderLike(Integer number,String fuzzy){
        //number = 1;
        //fuzzy = "6";
        return userService.getUserOrderLike(number,fuzzy);
    }

    @RequestMapping("/user/packageOrder")
    @ResponseBody
    //获取套餐订单表分页
    public List getPackageOrder(){
        return userService.getPackageOrder();
    }


    @RequestMapping("/user/packageOrderLimit")
    @ResponseBody
    //获取套餐订单表分页
    public Paging getPackageOrder(Integer number){
        return userService.getPackageOrder(number);
    }


    //获取套餐订单表分页的模糊查询
    @RequestMapping("/user/getPackageLike")
    @ResponseBody
    public Paging getPackageLike(Integer number,String fuzzy){
        //fuzzy = "6";
        //number = 1;
        return  userService.getPackageLike(number, fuzzy);
    }

    @RequestMapping("/user/score")
    @ResponseBody
    //积分查询
    public int getScore(Integer number){
        //number = 3;
        return userService.getScore(number);
    }
}
